﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using QuickApp;

namespace QA.Wpf
{
    public class QATreeViewItem : TreeViewItem
    {
        #region Private Fields

        //private IQACommand command = null;
        //private IQAParent qaParent = null;
        private QATreeView qaTreeView = null;

        public QATreeView QATreeView
        {
            get { return qaTreeView; }
            set { qaTreeView = value; }
        }
        private object currentObject = null;
        private bool hasBeenExpanded = false;

        #endregion Private Fields

        #region Public Constructors

        public QATreeViewItem()
        {

        }

        #endregion Public Constructors

        #region Public Properties

        //public IQACommand QACommand
        //{
        //    get { return command; }
        //    set { command = value; }
        //}
        public IQAParent QAParent
        {
            get
            {
                return currentObject as IQAParent;
            }
            private set
            {
                if (value != null)
                {
                    base.Header = value.DisplayName;
                    //Enables the expander option to be visible
                    this.Items.Add(new QATreeViewItem());
                }
            }
        }

        public object CurrentObject
        {
            get
            {
                return currentObject;
            }
            set
            {
                currentObject = value;
                if(currentObject != null)
                {
                    this.QAParent = currentObject as IQAParent;
                }
            }
        }

        #endregion Public Properties

        #region Protected Methods

        protected override void OnExpanded(System.Windows.RoutedEventArgs e)
        {
            IQAParent parent = this.QAParent;
            if(parent != null && !hasBeenExpanded)
            {
                hasBeenExpanded = true;
                this.Items.Clear();
                foreach (var kv in parent.Children)
                {
                    //Add the header - this does nothing but expand/contract
                    QATreeViewItem itm = new QATreeViewItem() { Header = kv.Key };
                    foreach(IQAChild child in kv.Value)
                        itm.Items.Add(new QATreeViewItem() { Header = child.DisplayName, CurrentObject = child, QATreeView = this.QATreeView});
                    this.Items.Add(itm);
                }
            }
            base.OnExpanded(e);
        }

        protected override void OnSelected(System.Windows.RoutedEventArgs e)
        {
            if (this.QATreeView != null)
                this.QATreeView.SelectedObject = this.currentObject;
            base.OnSelected(e);
        }

        //protected override void OnMouseDoubleClick(System.Windows.Input.MouseButtonEventArgs e)
        //{
        //    if (command != null)
        //        command.Execute();
        //}

        #endregion Protected Methods
    }
}
